package com.commerceerp.system.service.Impl;

import com.commerceerp.system.entity.GoodClassInfo;
import com.commerceerp.system.entity.GoodInfo;
import com.commerceerp.system.entity.GoodStorehouseInfo;
import com.commerceerp.system.entity.StorehouseInfo;
import com.commerceerp.system.mapper.GoodClassMapper;
import com.commerceerp.system.mapper.GoodMapper;
import com.commerceerp.system.mapper.GoodStorehouseMapper;
import com.commerceerp.system.mapper.StorehouseMapper;
import com.commerceerp.system.service.FactoryService;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private GoodStorehouseMapper goodStorehouseMapper;

    @Autowired
    private StorehouseMapper storehouseMapper;

    @Autowired
    private GoodClassMapper goodClassMapper;

    /**
     * 获取所有商品列表
     */
    @Override
    public List<Map> getAllSystemGood(String goodPlatformSku,String goodSystemSku, String goodSystemChname, String goodClass, String goodSellType, Integer goodCreateTimePX, Integer goodUpdTimePX,Integer goodSystemSkuPX, Integer goodPurchasePricePX) {

        List<Map> goodsList = goodMapper.getAllSystemGood(goodPlatformSku,goodSystemSku,goodSystemChname,goodClass,goodSellType,goodCreateTimePX,goodUpdTimePX,goodSystemSkuPX,goodPurchasePricePX);

        return goodsList;
    }

    /**
     *添加系统商品信息
     */
    @Override
    public Integer saveNewSystemGood(GoodInfo goodInfo) {

        goodInfo.setGoodCreateTime(DateUtil.getYMDHMSDate());
        goodInfo.setGoodId(StringUtils.getUUID());

        Integer success = goodMapper.saveNewSystemGood(goodInfo);

        GoodStorehouseInfo goodStorehouseInfo = new GoodStorehouseInfo();
        goodStorehouseInfo.setGsId(StringUtils.getUUID());
        goodStorehouseInfo.setGsCreateTime(DateUtil.getYMDHMSDate());
        goodStorehouseInfo.setGsGoodId(goodInfo.getGoodId());
        goodStorehouseInfo.setGsStorehouseId(goodInfo.getGoodStorehouseId());

        goodStorehouseMapper.saveGoodStorehouseInfoByGoodId(goodStorehouseInfo);

        return success;
    }


    /**
     * 获取仓库清单列表
     */
    @Override
    public List<Map> getAllGoodStorehouseInfo(String gsStorehouseId, String goodSystemChname, String goodSystemSku, String goodSellType, Integer gsCreateTimePX, Integer gsUpdTimePX, Integer gsGoodSkuPX, Integer gsAllNumberPX) {

        List<Map> gsList = goodStorehouseMapper.getAllGoodStorehouseInfo(gsStorehouseId,  goodSystemChname,  goodSystemSku,  goodSellType,  gsCreateTimePX,  gsUpdTimePX,  gsGoodSkuPX,  gsAllNumberPX);

        return gsList;
    }

    /**
     * 更新商品的仓库清单
     */
    @Override
    public Integer updGoodStorehouseInfo(GoodStorehouseInfo goodStorehouseInfo) {

        goodStorehouseInfo.setGsUpdTime(DateUtil.getYMDHMSDate());

        Integer success = goodStorehouseMapper.updGoodStorehouseInfo(goodStorehouseInfo);

        return success;
    }

    /**
     * 获取仓库信息列表
     */
    @Override
    public List<StorehouseInfo> getAllStorehouseInfo() {

        List<StorehouseInfo> storehouseinfos = storehouseMapper.getAllStorehouseInfo();

        return storehouseinfos;
    }

    /**
     * 新增仓库信息
     * @param storehouseInfo
     * @return
     */
    @Override
    public Integer saveNewStorehouseInfo(StorehouseInfo storehouseInfo) {

        storehouseInfo.setStorehouseId(StringUtils.getUUID());
        storehouseInfo.setStorehouseIsdefault("0");

        Integer success = storehouseMapper.saveNewStorehouseInfo(storehouseInfo);

        return success;
    }

    /**
     * 将平台sku绑定到系统商品
     */
    @Override
    public Integer updPlatformSkuToGood(String goodId, String goodPlatfromSku,String goodPlatformType) {

        String oldGoodPlatformSku = String.valueOf(goodMapper.getOldPaltformSkuAndType(goodId).get("goodPlatfromSku"));
        String oldGoodPlarfromType = String.valueOf(goodMapper.getOldPaltformSkuAndType(goodId).get("goodPlatfromType"));

        if(oldGoodPlatformSku == null || "".equals(oldGoodPlatformSku)){

        } else {
            goodPlatfromSku = oldGoodPlatformSku+","+goodPlatfromSku;
        }

        if(oldGoodPlarfromType == null || "".equals(oldGoodPlarfromType)){

        } else {
            goodPlatformType = oldGoodPlarfromType+","+goodPlatformType;
        }

        Integer success = goodMapper.updPlatformSkuToGood(goodId,goodPlatfromSku,goodPlatformType);

        return success;
    }


    /**
     * 获取商品绑定下拉菜单为绑定系统商品
     */
    @Override
    public List<GoodInfo> getNoBundingSystemSku() {

        List<GoodInfo> goods = goodMapper.getNoBundingSystemSku();

        return goods;
    }


    /**
     * 获取商品绑定下拉菜单为绑定系统商品
     */
    @Override
    public Map<String, Object> getGoodInfoByGoodId(String goodId) {

       Map<String,Object> good = goodMapper.getGoodInfoByGoodId(goodId);

        return good;
    }


    /**
     *编辑系统商品信息
     */
    @Override
    public Integer updSystemGoodInfoById(GoodInfo goodInfo) {

        Integer success = goodMapper.updSystemGoodInfoById(goodInfo);

        return success;
    }


    /**
     * 删除系统商品信息
     */
    @Override
    public Integer delSystemGoodInfoById(String goodId) {
        Integer success = 1;

        Integer gsAllNumber = goodMapper.getGsAllNumberIsZero(goodId);

        if(gsAllNumber == null ){
            success = -1;
        }else {
            if(gsAllNumber > 0 ){
                success = -1;
            } else {
                success = goodMapper.delSystemGoodInfoById(goodId);
            }
        }

        return success;
    }

    @Override
    public List<Map> getAllGoodClass() {

        List<Map> classes = goodClassMapper.getAllGoodClass();
        return classes;
    }

    @Override
    public Integer updGoodClass(String classIdAndNames) {

        String[] idAndNames = classIdAndNames.split(",");
        for(String idAndName : idAndNames){

            Integer index = idAndName.indexOf("_");
            Integer classId = Integer.parseInt(idAndName.substring(0,index));
            String className = idAndName.substring(index+1,idAndName.length());
            goodClassMapper.updGoodClass(classId,className);

        }

        return 1;
    }

    @Override
    public Integer saveGoodClass(GoodClassInfo goodClassInfo) {

        Integer success = goodClassMapper.saveGoodClass(goodClassInfo);
        return success;
    }

    @Override
    public Integer delGoodClass(String classId) {

        String[] ids = classId.split(",");
        for(String id : ids){

            goodClassMapper.delGoodClass(Integer.valueOf(id));

        }

        return 1;
    }

    @Override
    public Integer delStorehouseById(String storehouseId) {

        Integer success = storehouseMapper.delStorehouseById(storehouseId);

        return success;
    }


    /**
     * 新增商品仓库清单
     * @param goodStorehouseInfo
     * @return
     */
    @Override
    public Integer saveGoodStorehouseInfoByGoodId(GoodStorehouseInfo goodStorehouseInfo) {

        goodStorehouseInfo.setGsCreateTime(DateUtil.getYMDHMSDate());

        Integer success = goodStorehouseMapper.saveGoodStorehouseInfoByGoodId(goodStorehouseInfo);

        return success;
    }


}
